package cn.dnui_jzy913.dnuifood_16110100913.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.util.List;

import cn.dnui_jzy913.dnuifood_16110100913.Activity.soucuoActivity;
import cn.dnui_jzy913.dnuifood_16110100913.Adapter.ShopAdapter;
import cn.dnui_jzy913.dnuifood_16110100913.R;
import cn.dnui_jzy913.dnuifood_16110100913.bean.ShopBean;
import cn.dnui_jzy913.dnuifood_16110100913.listener.RetrofitListener;
import cn.dnui_jzy913.dnuifood_16110100913.model.ShopModel;

public class Fragment1 extends BaseFragment implements RetrofitListener<List<ShopBean>> {
    private LinearLayout linearLayout;
    private ShopAdapter shopAdapter;
    @Override
    protected int initlayout() {
        return R.layout.fragment1;
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        return super.onCreateView(inflater,container,savedInstanceState);
    }
    @Override
    void initViews() {
        linearLayout = view.findViewById(R.id.f1_sousuo);
        recyclerView = view.findViewById(R.id.rv01); //1.获取元素
    }

    @Override
    void initData() {
        ShopModel shopModel = new ShopModel();  //4.获取网络数据
        shopModel.getAllShops(this);
        Log.d("调试", "initData: 运行");
    }

    @Override
    void initEvents() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(  //2.创建布局管理容器
                getActivity(),
                LinearLayoutManager.VERTICAL,//设置不瀑布流
                false
        );//布局效果---垂直布局
        recyclerView.setLayoutManager(linearLayoutManager);//3.给recyclerview设置布局管理容器
        recyclerView.setItemAnimator(new DefaultItemAnimator());//设置动画效果

        linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(),soucuoActivity.class);
                startActivity(intent);
            }
        });
    }
    @Override
    public void onSuccess(List<ShopBean> shopBeans, int flag) {
        //5.设置适配器
        Log.d("调试", "onSuccess: 1");

        shopAdapter = new ShopAdapter(getActivity(),shopBeans,R.layout.shop_item);
        recyclerView.setAdapter(shopAdapter);
    }

    @Override
    public void onFalure() {
        Toast.makeText(getActivity(),"网络错误",Toast.LENGTH_SHORT).show();
    }
}

